package com.ruoyi.project.WebMoudle.streamShop.controller;

import java.math.BigDecimal;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.WebMoudle.wxOrder.domain.MoneySum;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.WebMoudle.streamShop.domain.StreamShop;
import com.ruoyi.project.WebMoudle.streamShop.service.IStreamShopService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 商家流水Controller
 * 
 * @author ruoyi
 * @date 2020-08-15
 */
@Controller
@RequestMapping("/streamShop/streamShop")
public class StreamShopController extends BaseController
{
    private String prefix = "streamShop/streamShop";

    @Autowired
    private IStreamShopService streamShopService;

    @RequiresPermissions("streamShop:streamShop:view")
    @GetMapping()
    public String streamShop()
    {
        return prefix + "/streamShop";
    }

    /**
     * 查询商家流水列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StreamShop streamShop)
    {
        startPage();
        List<StreamShop> list = streamShopService.selectStreamShopList(streamShop);
        return getDataTable(list);
    }

    /**
     * 查询返利账单
     */

    @PostMapping("/selectReturnList")
    @ResponseBody
    public TableDataInfo selectReturnList(StreamShop streamShop)
    {
        startPage();
        List<StreamShop> list = streamShopService.selectReturnList(streamShop);
        BigDecimal detailSum =streamShopService.queryReturnMoneySum(streamShop);
        if (!StringUtils.isNotNull(detailSum)){
            detailSum= new BigDecimal("0");
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        rspData.setDetailSum(detailSum);
        return rspData;
    }

    /**
     * 导出商家流水列表
     */
    @RequiresPermissions("streamShop:streamShop:export")
    @Log(title = "商家流水", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StreamShop streamShop)
    {
        List<StreamShop> list = streamShopService.selectStreamShopList(streamShop);
        ExcelUtil<StreamShop> util = new ExcelUtil<StreamShop>(StreamShop.class);
        return util.exportExcel(list, "streamShop");
    }

    /**
     * 新增商家流水
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商家流水
     */
    @RequiresPermissions("streamShop:streamShop:add")
    @Log(title = "商家流水", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StreamShop streamShop)
    {
        return toAjax(streamShopService.insertStreamShop(streamShop));
    }

    /**
     * 修改商家流水
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        StreamShop streamShop = streamShopService.selectStreamShopById(id);
        mmap.put("streamShop", streamShop);
        return prefix + "/edit";
    }

    /**
     * 修改保存商家流水
     */
    @RequiresPermissions("streamShop:streamShop:edit")
    @Log(title = "商家流水", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StreamShop streamShop)
    {
        return toAjax(streamShopService.updateStreamShop(streamShop));
    }

    /**
     * 删除商家流水
     */
    @RequiresPermissions("streamShop:streamShop:remove")
    @Log(title = "商家流水", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(streamShopService.deleteStreamShopByIds(ids));
    }
}
